package com.guiwu.gteway.industryapp.Device.View;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.guiwu.gteway.industryapp.Device.MyAdapter.MonitorExpandAdapter;
import com.guiwu.gteway.industryapp.Device.module.Monitor;
import com.guiwu.gteway.industryapp.R;
import com.guiwu.gteway.industryapp.util.MyLog;
import com.guiwu.gteway.industryapp.util.SoftKeyboardStateHelper;

/**
 * Created by qiaoguan on 2018/3/8. 监控点列表
 */

public class MonitorListFragment extends Fragment {
    private ExpandableListView expandList;
    private MonitorExpandAdapter adapter;
    private MonitorActivity activityParent;
    private float Xd,Yd;
    private static final String TAG = "MonitorListFragment";
    public static int sTimes;
  //  private static final String TAG = "MonitorActivity";
    private TextView eventText;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityParent = (MonitorActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.monitory_list_fragment,container,false);
        expandList = v.findViewById(R.id.monitory_expand_listview);
      //  adapter = new MonitorExpandAdapter(inflater,activityParent.getMonitorGroups());
     //   expandList.setAdapter(adapter);
//        expandList.setOnTouchListener(new widget.OnTouchListener() {
//            @Override
//            public boolean onTouch(widget v, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        Xd = event.getX();
//                        Yd = event.getY();
//                    case MotionEvent.ACTION_MOVE:
//                        if (Math.abs(event.getX() - Xd) >Math.abs(event.getY() - Yd)){
//                            expandList.getParent().requestDisallowInterceptTouchEvent(false);
//                        }else
//                            expandList.getParent().requestDisallowInterceptTouchEvent(true);
//                        break;
//                    case MotionEvent.ACTION_UP:
//                    case MotionEvent.ACTION_CANCEL:
//                        expandList.getParent().requestDisallowInterceptTouchEvent(false);
//                        break;
//                }
//                return false;
//            }
//        });

//        expandList.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//           //     expandList.getParent().requestDisallowInterceptTouchEvent(true);
//                return false;
//            }
//        });


        MonitorActivity.stopParentScroll(expandList);
        eventText = v.findViewById(R.id.event_name);
        return v;
    }

    public void updateUI(){

        if (adapter == null){
            adapter = new MonitorExpandAdapter(getLayoutInflater(),activityParent.getMonitorGroups(),activityParent.getmGateWay(),getContext());
            expandList.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        MyLog.i("monitorGroups","更新:" + adapter.getMonitorGroups().hashCode());

        adapter.setOnMonitorClickListener(new MonitorExpandAdapter.OnMonitorClickListener() {
            @Override
            public void onSwitch(final Monitor monitor , final boolean coil) {
                handleMonitorSwitch(monitor,coil);
            }

            @Override
            public void onEditStart(Monitor monitor) {
                activityParent.stopUpdate = true;
            }

            @Override
            public void onEditFinish(Monitor monitor , final int value) {
                activityParent.stopUpdate = false;
                handleMonitorEdit(monitor,value);
            }
        });

        //重发按钮监听
        adapter.setOnSwitchRepeatListener(new MonitorExpandAdapter.OnSwitchRepeatListener() {
            @Override
            public void onRepeat(Monitor monitor) {
                handleMonitorSwitch(monitor,!monitor.getSwitchState());
                MyLog.i(TAG,   monitor.getName() + " :执行repeat" + "重发状态" + !monitor.getSwitchState());
            }
        });

        //重发编辑监听
        adapter.setOnEditRepeatListener(new MonitorExpandAdapter.OnEditRepeatListener() {
            @Override
            public void onRepeat(Monitor monitor,int value) {
                MyLog.i(TAG,   monitor.getName() + " :执行repeat" + "重发状态" + value);
                handleMonitorEdit(monitor,value);
            }
        });



        //事件显示
        eventText.setText(activityParent.getEventInfo());

    }

    public static MonitorListFragment newInstance(String gateWaySn) {
        Bundle args = new Bundle();
        args.putString(MonitorActivity.GATEWAY_SN,gateWaySn);
        MonitorListFragment fragment = new MonitorListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    //主动控制开关函数
    private void handleMonitorSwitch(final Monitor monitor, final boolean coil){
      //  int value = monitor.getValueInt();
        try {
            monitor.sendCmd(activityParent.getmGateWay().getGateWayId(),activityParent.getmGateWay().getApiKey(),coil);
            monitor.setDataReturnListener(new Monitor.dateReturn() {
                @Override
                public void success(String response) {
                    MyLog.i(TAG,monitor.getName() +":监控点操作发送数据成功：" + monitor.getRepeatTime());
                    monitor.repeat();
                }

                @Override
                public void fail(String error) {
                    MyLog.i(TAG,"监控点操作失败：" + error);
                }
            });
        } catch (Exception e) {
            MyLog.i(TAG,"监控点操作失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

    //主动控制数值函数
    private void handleMonitorEdit(final Monitor monitor ,final int value){
        try {
            monitor.sendCmd(activityParent.getmGateWay().getGateWayId(),activityParent.getmGateWay().getApiKey(),value);
            monitor.setDataReturnListener(new Monitor.dateReturn() {
                @Override
                public void success(String response) {
                    MyLog.i(TAG,monitor.getName() +":监控点操作发送数据成功：" + monitor.getRepeatTime());
                    monitor.repeat();
                }

                @Override
                public void fail(String error) {
                    MyLog.i(TAG,"监控点操作失败：" + error);
                }
            });
        } catch (Exception e) {
            MyLog.i(TAG,"监控点操作失败：" + e.getMessage());
            e.printStackTrace();
        }
    }


}
